package com.taobao.txc.common.util.b;

import java.io.CharArrayWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.security.GeneralSecurityException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.net.ssl.SSLContext;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;

public class b
{
  private static HttpClient a = null;
  private static String b;
  
  public static String a(InputStream paramInputStream, String paramString)
  {
    return null == paramString ? a(new InputStreamReader(paramInputStream)) : a(new InputStreamReader(paramInputStream, paramString));
  }
  
  public static String a(Reader paramReader)
  {
    CharArrayWriter localCharArrayWriter = new CharArrayWriter();
    a(paramReader, localCharArrayWriter);
    return localCharArrayWriter.toString();
  }
  
  public static long a(Reader paramReader, Writer paramWriter)
  {
    char[] arrayOfChar = new char[4096];
    int i;
    for (long l = 0L; (i = paramReader.read(arrayOfChar)) >= 0; l += i) {
      paramWriter.write(arrayOfChar, 0, i);
    }
    return l;
  }
  
  private static CloseableHttpClient a()
  {
    try
    {
      SSLContext localSSLContext = new SSLContextBuilder().loadTrustMaterial(null, new c()).build();
      SSLConnectionSocketFactory localSSLConnectionSocketFactory = new SSLConnectionSocketFactory(localSSLContext, new d());
      return HttpClients.custom().setSSLSocketFactory(localSSLConnectionSocketFactory).build();
    }
    catch (GeneralSecurityException localGeneralSecurityException)
    {
      throw localGeneralSecurityException;
    }
  }
  
  public static String a(String paramString1, String paramString2, Integer paramInteger1, Integer paramInteger2)
  {
    Object localObject1 = null;
    HttpGet localHttpGet = new HttpGet(paramString1);
    String str = "";
    try
    {
      localHttpGet.addHeader("CS-ClientAddr", b);
      RequestConfig.Builder localBuilder = RequestConfig.custom();
      if (paramInteger1 != null) {
        localBuilder.setConnectTimeout(paramInteger1.intValue());
      }
      if (paramInteger2 != null) {
        localBuilder.setSocketTimeout(paramInteger2.intValue());
      }
      localHttpGet.setConfig(localBuilder.build());
      HttpResponse localHttpResponse = null;
      if (paramString1.startsWith("https"))
      {
        localObject1 = a();
        localHttpResponse = ((HttpClient)localObject1).execute(localHttpGet);
      }
      else
      {
        localObject1 = a;
        localHttpResponse = ((HttpClient)localObject1).execute(localHttpGet);
      }
      if (localHttpResponse.getStatusLine().getStatusCode() == 404) {
        throw new com.taobao.txc.common.b.c(com.taobao.txc.common.b.b.q);
      }
      if (localHttpResponse.getStatusLine().getStatusCode() >= 300) {
        throw new RuntimeException("http get failed:" + localHttpResponse.getStatusLine().getStatusCode());
      }
      str = a(localHttpResponse.getEntity().getContent(), paramString2);
    }
    finally
    {
      localHttpGet.releaseConnection();
      if ((paramString1.startsWith("https")) && (localObject1 != null) && ((localObject1 instanceof CloseableHttpClient))) {
        ((CloseableHttpClient)localObject1).close();
      }
    }
    return str;
  }
  
  public static String a(String paramString1, Map<String, String> paramMap, String paramString2, String paramString3, String paramString4, Integer paramInteger1, Integer paramInteger2)
  {
    Object localObject1 = null;
    HttpPost localHttpPost = new HttpPost(paramString1);
    String str = "";
    try
    {
      localHttpPost.addHeader("CS-ClientAddr", b);
      Object localObject3;
      if (paramMap != null)
      {
        localObject2 = paramMap.keySet().iterator();
        while (((Iterator)localObject2).hasNext())
        {
          localObject3 = (String)((Iterator)localObject2).next();
          localHttpPost.addHeader((String)localObject3, (String)paramMap.get(localObject3));
        }
      }
      if (StringUtils.isNotBlank(paramString2))
      {
        localObject2 = new StringEntity(paramString2, ContentType.create(paramString3, paramString4));
        localHttpPost.setEntity((HttpEntity)localObject2);
      }
      Object localObject2 = RequestConfig.custom();
      if (paramInteger1 != null) {
        ((RequestConfig.Builder)localObject2).setConnectTimeout(paramInteger1.intValue());
      }
      if (paramInteger2 != null) {
        ((RequestConfig.Builder)localObject2).setSocketTimeout(paramInteger2.intValue());
      }
      localHttpPost.setConfig(((RequestConfig.Builder)localObject2).build());
      if (paramString1.startsWith("https"))
      {
        localObject1 = a();
        localObject3 = ((HttpClient)localObject1).execute(localHttpPost);
      }
      else
      {
        localObject1 = a;
        localObject3 = ((HttpClient)localObject1).execute(localHttpPost);
      }
      if (((HttpResponse)localObject3).getStatusLine().getStatusCode() >= 300) {
        throw new RuntimeException(String.format("http post failed:%d %s", new Object[] { Integer.valueOf(((HttpResponse)localObject3).getStatusLine().getStatusCode()), ((HttpResponse)localObject3).getStatusLine().getReasonPhrase() }));
      }
      str = a(((HttpResponse)localObject3).getEntity().getContent(), paramString4);
    }
    finally
    {
      localHttpPost.releaseConnection();
      if ((paramString1.startsWith("https")) && (localObject1 != null) && ((localObject1 instanceof CloseableHttpClient))) {
        ((CloseableHttpClient)localObject1).close();
      }
    }
    return str;
  }
  
  public static String a(String paramString, int paramInt)
  {
    return a(paramString, "UTF-8", Integer.valueOf(paramInt), Integer.valueOf(60000));
  }
  
  public static String a(String paramString1, Map<String, String> paramMap, String paramString2)
  {
    return a(paramString1, paramMap, paramString2, "json", "UTF-8", Integer.valueOf(10000), Integer.valueOf(60000));
  }
  
  static
  {
    PoolingHttpClientConnectionManager localPoolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
    localPoolingHttpClientConnectionManager.setMaxTotal(128);
    localPoolingHttpClientConnectionManager.setDefaultMaxPerRoute(10);
    a = HttpClients.custom().setConnectionManager(localPoolingHttpClientConnectionManager).build();
    try
    {
      b = com.taobao.txc.common.d.a();
    }
    catch (Exception localException)
    {
      b = "0.0.0.0";
    }
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.common.util.b.b
 * JD-Core Version:    0.7.0.1
 */